import request from '@utils/request';
import * as actionTypes from './actionType';
import { message } from 'antd';

//获取所有菜单
export const getAllMenuRequest = (values) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/menu/getAllMenus', {
            cmd: 'getAllMenus',
            type: 'request',
            request: {
               ...values 
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message;
                dispatch(getAllMenuUpdateReducer(result))
            } else {
                message.error('获取数据失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const getAllMenuUpdateReducer = (getAllMenu) => ({
    type: actionTypes.ALLMENU,
    getAllMenu
})
//新增信息
export const addMenuRequest = (value) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/menu/addMenu', {
            cmd: 'addMenu',
            type: 'request',
            request: {
                ...value
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('添加成功', 1);
                dispatch(getAllMenuRequest())
            } else {
                message.error('添加失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
//删除数据
export const deleteMenuRequest = (value) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/menu/deleteMenu', {
            cmd: 'deleteMenu',
            type: 'request',
            request: {
                ...value
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('已删除!', 1);
                dispatch(getAllMenuRequest())
            } else {
                message.error('添加失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
//操作
export const getOptionRequest = (value) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/menu/getOption', {
            cmd: 'getOption',
            type: 'request',
            request: {
                ...value
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                message.success('操作成功!', 1);
                dispatch(getAllMenuRequest())
            } else {
                message.error('添加失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}

//支付
export const alipayRequest = (value) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/pay/alipay', {
            cmd: 'alipay',
            type: 'request',
            request: {
                ...value
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let qrcode = res.message.qrcode
                let out_trade_no = res.message.out_trade_no
                dispatch(alipayUpdateReducer(qrcode, out_trade_no))
            } else {
                message.error('支付失败！' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const alipayUpdateReducer = (qrcode, out_trade_no) => ({
    type: actionTypes.ALIPAY,
    qrcode, out_trade_no
})
//查询支付状态
export const queryRequest = (value) => {
    return (dispatch) => {
        request('POST', '/api/server/web/rest/pay/query', {
            cmd: 'query',
            type: 'request',
            request: {
                ...value
            }
        }).then(obj => {
            let res = obj.response;
            if (res.res) {
                let result = res.message.status;
                dispatch(queryStatusUpdateReducer(result))

            } else {
                message.error('添加失败' + res.exception, 1);
            }
        }).catch(err => console.log(err));
    }
}
const queryStatusUpdateReducer = (queryStatus) => ({
    type: actionTypes.QUERTSTATUS,
    queryStatus
})